package com.blog.blog.controller;

import com.blog.blog.common.ApiBaseResponse;
import com.blog.blog.service.I18nService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.LocaleResolver;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Map;

/**
 * 国际化控制器
 */
@Tag(name = "国际化", description = "多语言国际化支持")
@RestController
@RequestMapping("/api/i18n")
@RequiredArgsConstructor
public class I18nController {
    
    private final I18nService i18nService;
    private final LocaleResolver localeResolver;
    
    @Operation(summary = "获取支持的语言列表")
    @GetMapping("/languages")
    public ApiBaseResponse<Map<String, String>> getSupportedLanguages() {
        return ApiBaseResponse.success(i18nService.getSupportedLanguages());
    }
    
    @Operation(summary = "获取当前语言的所有消息")
    @GetMapping("/messages")
    public ApiBaseResponse<Map<String, String>> getAllMessages() {
        return ApiBaseResponse.success(i18nService.getAllMessages());
    }
    
    @Operation(summary = "获取指定语言的所有消息")
    @GetMapping("/messages/{locale}")
    public ApiBaseResponse<Map<String, String>> getAllMessages(@PathVariable String locale) {
        Locale targetLocale = parseLocale(locale);
        return ApiBaseResponse.success(i18nService.getAllMessages(targetLocale));
    }
    
    @Operation(summary = "获取当前语言")
    @GetMapping("/current")
    public ApiBaseResponse<Map<String, Object>> getCurrentLocale() {
        Locale currentLocale = LocaleContextHolder.getLocale();
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("locale", currentLocale.toString());
        result.put("language", currentLocale.getLanguage());
        result.put("country", currentLocale.getCountry());
        result.put("displayName", currentLocale.getDisplayName());
        return ApiBaseResponse.success(result);
    }
    
    @Operation(summary = "切换语言")
    @PostMapping("/switch")
    public ApiBaseResponse<String> switchLanguage(
            @RequestParam String locale,
            HttpServletRequest request,
            HttpServletResponse response) {
        
        try {
            Locale targetLocale = parseLocale(locale);
            localeResolver.setLocale(request, response, targetLocale);
            
            // 验证切换是否成功
            String message = i18nService.getMessage("common.success");
            return ApiBaseResponse.success("语言切换成功: " + message);
            
        } catch (Exception e) {
            return ApiBaseResponse.error(400, "不支持的语言: " + locale);
        }
    }
    
    @Operation(summary = "获取指定消息")
    @GetMapping("/message/{key}")
    public ApiBaseResponse<String> getMessage(@PathVariable String key) {
        String message = i18nService.getMessage(key);
        return ApiBaseResponse.success(message);
    }
    
    @Operation(summary = "获取带参数的消息")
    @PostMapping("/message/{key}")
    public ApiBaseResponse<String> getMessageWithArgs(
            @PathVariable String key,
            @RequestBody(required = false) Object[] args) {
        
        String message = i18nService.getMessage(key, args);
        return ApiBaseResponse.success(message);
    }
    
    private Locale parseLocale(String localeString) {
        if (localeString == null || localeString.isEmpty()) {
            return Locale.getDefault();
        }
        
        String[] parts = localeString.split("_");
        if (parts.length == 1) {
            return new Locale(parts[0]);
        } else if (parts.length == 2) {
            return new Locale(parts[0], parts[1]);
        } else if (parts.length >= 3) {
            return new Locale(parts[0], parts[1], parts[2]);
        }
        
        return Locale.getDefault();
    }
}
